package com.mine.photohandler.activity;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.mine.photohandler.R;
import com.mine.photohandler.adapter.AlbumAdapter;
import com.mine.photohandler.base.BaseAppCompatActivity;
import com.mine.photohandler.util.Bimp;
import com.mine.photohandler.util.ImageItem;
import com.mine.photohandler.util.PublicWay;

import java.util.ArrayList;

/**
 * 这个是显示一个文件夹里面的所有图片时的界面
 *
 */
public class ShowAllPhoto extends BaseAppCompatActivity implements View.OnClickListener {
	private TextView mTv_top_title;
	private TextView mTv_ok;
	private GridView mGv_album;

	private String folderName;
	private int position;

	public ArrayList<ImageItem> dataList = new ArrayList<>();
	private AlbumAdapter mAdapter;

	@Override
	protected int contentView() {
		return R.layout.plugin_activity_show_all;
	}

	@Override
	protected void initViews() {
		setStatusBar(Color.BLACK);
		mTv_top_title = (TextView) findViewById(R.id.tv_top_title);
		mTv_ok = (TextView) findViewById(R.id.tv_ok);
		mGv_album = (GridView) findViewById(R.id.gv_album);

		mAdapter = new AlbumAdapter(this, true, R.layout.plugin_camera_select_imageview);
		mGv_album.setAdapter(mAdapter);
	}

	@Override
	protected void initEvents() {
		findViewById(R.id.rl_back).setOnClickListener(this);
		findViewById(R.id.rl_right).setOnClickListener(this);
		findViewById(R.id.fl_preview).setOnClickListener(this);
		findViewById(R.id.fl_ok).setOnClickListener(this);

         /*
            选中
          */
		mGv_album.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

				// 判断是否已经选够
				if (Bimp.tempSelectBitmap.size() >= PublicWay.num) {
					showMsg(R.string.only_choose_num);
				} else {
					mAdapter.changeSel(position);
					Bimp.tempSelectBitmap.add(dataList.get(position));
					mTv_ok.setText("OK" + "(" + Bimp.tempSelectBitmap.size() + "/" + PublicWay.num + ")");
				}

			}
		});

         /*
            取消选中
         */
		mAdapter.setOnItemCancledListener(new AlbumAdapter.OnItemCancledListener() {
			@Override
			public void onItemCancled(int position) {
				mAdapter.cleanSel(position);
				Bimp.tempSelectBitmap.remove(dataList.get(position));
				mTv_ok.setText("OK" + "(" + Bimp.tempSelectBitmap.size()
						+ "/" + PublicWay.num + ")");
			}
		});
	}

	@Override
	protected void loadDatas(Intent intent) {
		if (intent != null) {
			if (intent.getExtras() != null) {
				folderName = intent.getExtras().getString("folderName");
				position = intent.getExtras().getInt("folderName_pos");

				mTv_top_title.setText(folderName);

				if (position != -1) {
					dataList = (ArrayList<ImageItem>) Bimp.helper.getImagesBucketList(false).get(position).imageList;

					mAdapter.setDatas(dataList);
				}
			}
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		mTv_ok.setText("OK" + "(" + Bimp.tempSelectBitmap.size() + "/" + PublicWay.num + ")");
	}

	@Override
	public void onClick(View view) {
		int id = view.getId();
		Intent intent = new Intent();
		if (id == R.id.rl_back) {  //返回相册
			startActivity(ImageFile.class, true);
		} else if (id == R.id.rl_right) { // 取消
			finish();
		} else if (id == R.id.fl_preview) { // 预览
			if (Bimp.tempSelectBitmap.size() > 0) {
				intent.putExtra("position", "1");
				intent.setClass(this, GalleryActivity.class);
				startActivity(intent);
				finish();
			}
		} else if (id == R.id.fl_ok) { // 确定
			overridePendingTransition(R.anim.activity_translate_in,
					R.anim.activity_translate_out);
			finish();
		}
	}

}
